<?php
namespace app\admin\controller;
use think\Db;
use app\common\controller\AdminBaseController;
class RoleController extends AdminBaseController
{
	 protected $auth_group_model;

	 protected function _initialize() {
        parent::_initialize();
      	$list = Db::name('auth_group') ->order(['sort' => 'ASC', 'id' => 'ASC'])->select();
       	$this->assign('list', $list);


        
    }
    
	public function index(){
		 
        return $this->fetch();
    }
	
	/*
	 
	 * 添加角色
	 * 
	 * */
	
	
	public function add_role(){
		if(input('post.')){
            $param = input('post.');
            $param['add_time']=now_time();
            if(Db::name('auth_group')->insert($param)){
            	return	toSuccess('添加成功'); 
           	 	}else{
            	return toError(408,'添加失败，请重新添加');
            }
            
        }

        return $this->fetch();
		
	}
	

    /**
     * [edit_rule 编辑菜单]
     * @return [type] [description]
     */
    public function edit_role()
    {
        if(input('post.')){
            $param = input('post.');
            if(Db::name('auth_group')->update($param)){
            	return	toSuccess('修改成功');
            }else{
            	 return toError(408,'修改失败，请重新修改');
            }
        }
        $id = input('param.id');
        $this->assign('menu',Db::name('auth_group')->find($id));
        return $this->fetch();
    }

	
	

    /**
     * [roleDel 删除]
     * @return [type] [description]
     */
    public function delete()
    {
        $id = input('param.id');
        if (Db::name('auth_group')->where('id',$id)->delete()) {
           return	toSuccess('删除成功');
        } else {
            return toError(408,'删除失败，请重新删除');
        }
    }
    
    
    
        /**
     * [rule_state 菜单状态]
     * @return [type] [description]
     */
    public function rule_state()
    {
        $id = input('param.id');
        $status = Db::name('auth_rule')->where('id',$id)->value('status');//判断当前状态
        if($status==1)
        {
            $flag = Db::name('auth_rule')->where('id',$id)->setField(['status'=>0]);
            return json(['code' => 1, 'data' => $flag['data'], 'msg' => '已禁止']);
        }
        else
        {
            $flag = Db::name('auth_rule')->where('id',$id)->setField(['status'=>1]);
            return json(['code' => 0, 'data' => $flag['data'], 'msg' => '已开启']);
        }
    
    }
    

// 获取管理员的权限列表
 public function select_role($id){
 	$p=input('post.')?input('post.'):"";
		if($p==""){
		$auth_group_data = Db::name('auth_group')->field('rules')->where('id',$id)->find();   	
		$s2=implode(',',$auth_group_data);
		$auth_rules      = explode(',', $s2);
		$auth_rule_list  = Db::name('auth_rule')->field('id,pid,title')->select();
				foreach ($auth_rule_list as $key => $value) {
         					in_array($value['id'], $auth_rules) && $auth_rule_list[$key]['checked'] = true;
    			 }
		 return	$this->fetch('power',['list'=>json_encode($auth_rule_list),'id'=>$id]);
		}else{
			$p=input('post.');
			if($p['rule_ids']=="+1"){
				$char="";
			}else{
				$char = implode(",", $p['rule_ids']);
			}
			if(Db::name('auth_group')->where('id',$p['id'])->update(['rules'=>$char])){
				return	toSuccess('授权成功');
			}else{
				return toError(408,'授权失败');
			}
		}
    	
    }

}
?>